home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2006 April / DPPRO0406DVD.ISO / Essentials / Programming / Eclipse SDK / eclipse-SDK-3.1.1-win32.exe / eclipse / plugins / org.apache.ant_1.6.5 / bin / complete-ant-cmd.pl < prev    next >
Encoding:
Perl Script  |  2005-09-29  |  2.9 KB  |  114 lines

  1. #!/usr/bin/perl
  2. #
  3. # Copyright 2001,2004 The Apache Software Foundation
  4. #
  5. #  Licensed under the Apache License, Version 2.0 (the "License");
  6. #  you may not use this file except in compliance with the License.
  7. #  You may obtain a copy of the License at
  8. #
  9. #      http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. #  Unless required by applicable law or agreed to in writing, software
  12. #  distributed under the License is distributed on an "AS IS" BASIS,
  13. #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. #  See the License for the specific language governing permissions and
  15. #  limitations under the License.
  16. #
  17. # A script to allow Bash or Z-Shell to complete an Ant command-line.  
  18. #
  19. # To install for Bash 2.0 or better, add the following to ~/.bashrc:
  20. #     $ complete -C complete-ant-cmd ant build.sh
  21. #
  22. # To install for Z-Shell 2.5 or better, add the following to ~/.zshrc:
  23. #
  24. #     function ant_complete () {
  25. #         local args_line args
  26. #         read -l args_line
  27. #         set -A args $args_line
  28. #         set -A reply $(COMP_LINE=$args_line complete-ant-cmd ${args[1]} $1)
  29. #     }
  30. #     compctl -K ant_complete ant build.sh
  31. #     
  32. # @author Mike Williams <mikew@cortexebusiness.com.au>
  33.  
  34. my $cmdLine = $ENV{'COMP_LINE'};
  35. my $antCmd = $ARGV[0];
  36. my $word = $ARGV[1];
  37.  
  38. my @completions;
  39. if ($word =~ /^-/) {
  40.     list( restrict( $word, getArguments() ));
  41. } elsif ($cmdLine =~ /-(f|buildfile)\s+\S*$/) {
  42.     list( getBuildFiles($word) );
  43. } else {
  44.     list( restrict( $word, getTargets() ));
  45. }
  46.  
  47. exit(0);
  48.  
  49. sub list {
  50.     for (@_) {
  51.         print "$_\n";
  52.     }
  53. }
  54.  
  55. sub restrict {
  56.     my ($word, @completions) = @_;
  57.     grep( /^\Q$word\E/, @completions );
  58. }
  59.  
  60. sub getArguments {
  61.     qw(-buildfile -debug -emacs -f -find -help -listener -logfile 
  62.        -logger -projecthelp -quiet -verbose -version); 
  63. }
  64.  
  65.  
  66. sub getBuildFiles {
  67.     my ($word) = @_;
  68.     grep( /\.xml$/, glob( "$word*" ));
  69. }
  70.  
  71. sub getTargets {
  72.  
  73.     # Look for build-file
  74.     my $buildFile = 'build.xml';
  75.     if ($cmdLine =~ /-(f|buildfile)\s+(\S+)/) {
  76.         $buildFile = $2;
  77.     }
  78.     return () unless (-f $buildFile);
  79.  
  80.     # Run "ant -projecthelp" to list targets.  Keep a cache of results in a
  81.     # cache-file.
  82.     my $cacheFile = $buildFile;
  83.     $cacheFile =~ s|(.*/)?(.*)|${1}.ant-targets-${2}|;
  84.     if ((!-e $cacheFile) || (-M $buildFile) < (-M $cacheFile)) {
  85.         open( CACHE, '>'.$cacheFile ) || die "can\'t write $cacheFile: $!\n";
  86.         open( HELP, "$antCmd -projecthelp -f '$buildFile'|" ) || return(); 
  87.         my %targets;
  88.         while( <HELP> ) {
  89.             if (/^\s+(\S+)/) {
  90.                 $targets{$1}++;
  91.             }
  92.         }
  93.         my @targets = sort keys %targets;
  94.         for (@targets) { print CACHE "$_\n"; }
  95.         return @targets;
  96.     }
  97.     
  98.     # Read the target-cache
  99.     open( CACHE, $cacheFile ) || die "can\'t read $cacheFile: $!\n";
  100.     my @targets;
  101.     while (<CACHE>) {
  102.         chop;
  103.         s/\r$//;  # for Cygwin
  104.         push( @targets, $_ );
  105.     }
  106.     close( CACHE );
  107.     @targets;
  108.  
  109. }
  110.  
  111.  
  112.  
  113.